//
//  DM_SQLCreateTableBuilder.swift
//  构建者模式_ORM_SWIFT
//
//  Created by 王苏进 on 17/5/17.
//  Copyright © 2017年 macOS. All rights reserved.
//

import UIKit

/*
 
 create table stuMarks
 (
 ExamNo      int     identity(1,1) primary key,
 stuNo       char(6) not null,
 writtenExam int     not null,
 LabExam     int     not null
 )
 */

class DM_SQLCreateTableBuilder: DM_SQLBuilder {
    
    
    
    func create() -> DM_SQLCreateTableBuilder {
    
        sql.append("create ")
        
        return self
    }
    
    override func talbe(_ table: String) -> DM_SQLBuilder {
        self.sql.append("table IF NOT EXISTS ")
        
        return super.talbe(table) as! DM_SQLCreateTableBuilder
    }
    
    
    
    //Int 类型的字段
     func colums_Int(_ column: String) -> DM_SQLCreateTableBuilder {
        
        return column_all(column, type: "integer")
 
    }
    
    
    //浮点 类型的字段
    func colums_Real(_ column: String) -> DM_SQLCreateTableBuilder {
        
       return column_all(column, type: "real")
  
    }
    
    //字符串 类型的字段
    func colums_text(_ column: String) -> DM_SQLCreateTableBuilder {
        
         return column_all(column, type: "text")
    
    }
    
    func comma() -> DM_SQLCreateTableBuilder {
        sql.append(", ")
        return self
    }
    
    
     func column_all(_ column: String, type: String) -> DM_SQLCreateTableBuilder {
        sql.append(column)
        sql.append(" \(type) ")
        
        return self

    }
    
     func columns_all(_ columns: [String : String]) -> DM_SQLCreateTableBuilder {
        
        var builder = self
        
        sql.append("( ")
        var index = 0
        for item in columns {
            builder = column_all(item.key, type: item.value)
            index += 1
            if index < columns.count {
               builder = comma()
            }
        }
        
        sql.append(") ")
        
        return builder
        
    }

     func getColumnType(propertyType: String) -> String {
        if propertyType == "Int" {
            
            return "integer"
        }
        
        return ""
        
    }
   
    
    
    //是否是主键
    func primaryKey() -> DM_SQLCreateTableBuilder {
        
        sql.append("primary key ")
        return self
    }
    
    //自增长
    func autoIncrement() -> DM_SQLCreateTableBuilder {
        
        sql.append("autoincrement ")
        return self
    }
    
    //Not Null
    
    func notNull() -> DM_SQLCreateTableBuilder {
        sql.append("not null ")
        return self
    }
    
    func defaultValue(_ def: Any) -> DM_SQLCreateTableBuilder {
        
        sql.append("default ")
        appendValue(def)
        
        return self
    }
    
    
    

}
